import org.json.JSONObject;

public class RecursiveTask {
    public static void main(String[] args) {
        // 创建根节点
        JSONObject root1 = new JSONObject();
        root1.put("name", "智能手机");

        JSONObject root2 = new JSONObject();
        root2.put("name", "电脑");

        JSONObject root3 = new JSONObject();
        root3.put("name", "家用电器");

        // 创建根节点1的子节点
        JSONObject smartphone_sub1 = new JSONObject();
        smartphone_sub1.put("name", "苹果");

        JSONObject smartphone_sub2 = new JSONObject();
        smartphone_sub2.put("name", "三星");

        // 创建苹果子节点的子集
        JSONObject apple_sub1 = new JSONObject();
        apple_sub1.put("name", "iPhone 13");

        JSONObject apple_sub2 = new JSONObject();
        apple_sub2.put("name", "iPhone 12");

        // 创建三星子节点的子集
        JSONObject samsung_sub1 = new JSONObject();
        samsung_sub1.put("name", "Galaxy S21");

        JSONObject samsung_sub2 = new JSONObject();
        samsung_sub2.put("name", "Galaxy Note 20");

        // 将苹果和三星的子集添加到它们各自的父节点中
        smartphone_sub1.put("subcategories", new JSONObject().put("0", apple_sub1).put("1", apple_sub2));
        smartphone_sub2.put("subcategories", new JSONObject().put("0", samsung_sub1).put("1", samsung_sub2));

        // 将苹果和三星添加到智能手机根节点中
        root1.put("subcategories", new JSONObject().put("0", smartphone_sub1).put("1", smartphone_sub2));

        // 创建根节点2的子节点
        JSONObject computer_sub1 = new JSONObject();
        computer_sub1.put("name", "笔记本电脑");

        JSONObject computer_sub2 = new JSONObject();
        computer_sub2.put("name", "台式机");

        // 创建笔记本电脑子节点的子集
        JSONObject laptop_sub1 = new JSONObject();
        laptop_sub1.put("name", "MacBook Pro");

        JSONObject laptop_sub2 = new JSONObject();
        laptop_sub2.put("name", "ThinkPad");

        // 创建台式机子节点的子集
        JSONObject desktop_sub1 = new JSONObject();
        desktop_sub1.put("name", "iMac");

        JSONObject desktop_sub2 = new JSONObject();
        desktop_sub2.put("name", "Dell XPS");

        // 将笔记本电脑和台式机的子集添加到它们各自的父节点中
        computer_sub1.put("subcategories", new JSONObject().put("0", laptop_sub1).put("1", laptop_sub2));
        computer_sub2.put("subcategories", new JSONObject().put("0", desktop_sub1).put("1", desktop_sub2));

        // 将笔记本电脑和台式机添加到电脑根节点中
        root2.put("subcategories", new JSONObject().put("0", computer_sub1).put("1", computer_sub2));

        // 创建根节点3的子节点
        JSONObject appliance_sub1 = new JSONObject();
        appliance_sub1.put("name", "冰箱");

        JSONObject appliance_sub2 = new JSONObject();
        appliance_sub2.put("name", "洗衣机");

        // 创建冰箱子节点的子集
        JSONObject fridge_sub1 = new JSONObject();
        fridge_sub1.put("name", "西门子");

        JSONObject fridge_sub2 = new JSONObject();
        fridge_sub2.put("name", "松下");

        // 创建洗衣机子节点的子集
        JSONObject washer_sub1 = new JSONObject();
        washer_sub1.put("name", "海尔");

        JSONObject washer_sub2 = new JSONObject();
        washer_sub2.put("name", "LG");

        // 将冰箱和洗衣机的子集添加到它们各自的父节点中
        appliance_sub1.put("subcategories", new JSONObject().put("0", fridge_sub1).put("1", fridge_sub2));
        appliance_sub2.put("subcategories", new JSONObject().put("0", washer_sub1).put("1", washer_sub2));

        // 将冰箱和洗衣机添加到家用电器根节点中
        root3.put("subcategories", new JSONObject().put("0", appliance_sub1).put("1", appliance_sub2));

        // 将所有根节点添加到最终的JSON对象中
        JSONObject finalResult = new JSONObject();
        finalResult.put("0", root1).put("1", root2).put("2", root3);

        // 打印结果
        System.out.println(finalResult.toString(4)); // 使用缩进4个空格打印
    }
}
